package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.PaymentRecords;
import com.example.mall.dto.PaymentRecordsDto;
import com.example.mall.mapper.PaymentRecordsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.PaymentRecordsQuery;
import com.example.mall.service.IPaymentRecordsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 支付记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class PaymentRecordsServiceImpl extends ServiceImpl<PaymentRecordsMapper, PaymentRecords> implements IPaymentRecordsService {

    @Autowired
    private PaymentRecordsMapper paymentRecordsMapper;

    @Override
    public PageDTO<PaymentRecordsDto> queryPage(PaymentRecordsQuery paymentRecordsQuery) {
        if (paymentRecordsQuery == null) {
            paymentRecordsQuery = new PaymentRecordsQuery();
        }
        Page<PaymentRecords> page = paymentRecordsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<PaymentRecords> cartPage = lambdaQuery()
                .like(paymentRecordsQuery.getPaymentId()!= null, PaymentRecords::getPaymentId, paymentRecordsQuery.getPaymentId())
                .like(paymentRecordsQuery.getOrderId()!= null, PaymentRecords::getOrderId, paymentRecordsQuery.getOrderId())
                .like(paymentRecordsQuery.getUserId()!= null, PaymentRecords::getUserId, paymentRecordsQuery.getUserId())
                .like(paymentRecordsQuery.getMethodId()!= null, PaymentRecords::getMethodId, paymentRecordsQuery.getMethodId())
                .like(paymentRecordsQuery.getAmount()!= null, PaymentRecords::getAmount, paymentRecordsQuery.getAmount())
                .like(paymentRecordsQuery.getTransactionId()!= null, PaymentRecords::getTransactionId, paymentRecordsQuery.getTransactionId())
                .like(paymentRecordsQuery.getPaymentStatus()!= null, PaymentRecords::getPaymentStatus, paymentRecordsQuery.getPaymentStatus())
                .orderByDesc(PaymentRecords::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, PaymentRecordsDto.class);
    }

    @Override
    public PaymentRecords queryPaymentRecordsById(Long paymentId) {
        AssertUtil.isNotNull(paymentId, "支付记录ID不能为空");
        return paymentRecordsMapper.selectById(paymentId);
    }

    @Override
    public List<PaymentRecords> queryPaymentRecordsList() {
        return this.list();
    }

    @Override
    public void insert(PaymentRecordsDto paymentRecordsDto) {
        PaymentRecords paymentRecords = new PaymentRecords();
        CopyUtils.copyProperties(paymentRecordsDto, paymentRecords);
        paymentRecords.setPaymentId(CodeGenerateUtils.generateUniqueId());
        paymentRecords.setCreateDate(new Date());
        paymentRecords.setUpdateDate(new Date());
        paymentRecords.setPaymentStatus(BaseConstants.NOT_USED);
        paymentRecordsMapper.insert(paymentRecords);
    }

    @Override
    public void update(PaymentRecordsDto paymentRecordsDto) {
        AssertUtil.isNotNull(paymentRecordsDto.getPaymentId(), "支付记录ID不能为空");
        PaymentRecords paymentRecords = paymentRecordsMapper.selectById(paymentRecordsDto.getPaymentId());
        AssertUtil.isNotNull(paymentRecords, "支付记录不存在");
        CopyUtils.copyProperties(paymentRecordsDto, paymentRecords);
        paymentRecords.setUpdateDate(new Date());
        paymentRecordsMapper.updateById(paymentRecords);
    }

    @Override
    public void delete(Long paymentId) {
        AssertUtil.isNotNull(paymentId, "支付记录ID不能为空");
        PaymentRecords paymentRecords = paymentRecordsMapper.selectById(paymentId);
        AssertUtil.isNotNull(paymentRecords, "支付记录不存在");
        paymentRecords.setPaymentStatus(BaseConstants.CANCEL);
        paymentRecords.setUpdateDate(new Date());
        paymentRecordsMapper.updateById(paymentRecords);
    }

    @Override
    public void deleteBatch(List<Long> paymentIds) {
        AssertUtil.isNotNull(paymentIds, "支付记录ID列表不能为空");
        List<PaymentRecords> paymentRecords = paymentRecordsMapper.selectBatchIds(paymentIds);
        AssertUtil.isNotNull(paymentRecords, "支付记录不存在");
        for (PaymentRecords paymentRecord : paymentRecords) {
            paymentRecord.setPaymentStatus(BaseConstants.CANCEL);
            paymentRecord.setUpdateDate(new Date());
            paymentRecordsMapper.updateById(paymentRecord);
        }
    }
}
